//
//  SuggestViewController.m
//  FreshBee
//
//  Created by 姜 祥云 on 16/10/15.
//  Copyright © 2016年 iOS013. All rights reserved.
//

#import "SuggestViewController.h"

@interface SuggestViewController ()<UITextViewDelegate>
@property (nonatomic,weak)UITextView *putSuggest;
@property (nonatomic,weak)UILabel *suggest;

@end

@implementation SuggestViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUi];
}
-(void)setupUi
{
   self.title=@"意见反馈";
    UIBarButtonItem *rightBarBtn=[[UIBarButtonItem alloc]initWithTitle:@"发送" style:UIBarButtonItemStyleDone target:self action:@selector(sender)];
    self.navigationItem.rightBarButtonItem=rightBarBtn;
    [self.navigationController.navigationBar setTintColor:[UIColor lightGrayColor]];
    self.view.backgroundColor=[UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1];
    UILabel *suggestInfo = [[UILabel alloc]init];
    suggestInfo.text=@"你的批评和建议能帮助我们更好的完善产品,请留下你的宝贵意见!";
    suggestInfo.numberOfLines=0;
    suggestInfo.textColor=[UIColor darkGrayColor];
    [self.view addSubview:suggestInfo];
    [suggestInfo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(70);
        make.left.equalTo(self.view).offset(5);
        make.right.equalTo(self.view).offset(-5);
    }];
    
    UITextView *putSuggest=[[UITextView alloc]init];
    
    [self.view addSubview:putSuggest];
    [putSuggest mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(suggestInfo.mas_bottom).offset(10);
        make.right.equalTo(self.view).offset(-10);
        make.left.equalTo(self.view).offset(10);
        make.height.equalTo(@(self.view.bounds.size.height/3.0));
        
        
    }];
    UILabel *Info=[[UILabel alloc]init];
    Info.text=@"请输入宝贵意见(300字以内)";
    Info.textColor=[UIColor darkGrayColor];
    [putSuggest addSubview:Info];
    [Info mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(putSuggest).offset(5);
    }];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textViewDidChange:) name:UITextViewTextDidChangeNotification object:nil];
    self.putSuggest.delegate = self;
    
   
    self.putSuggest=putSuggest;
    self.suggest=Info;
    
}

-(void)textViewDidChange:(UITextView *)textView{
    if(_putSuggest.text.length!=0){
        self.suggest.alpha=0;
    }else{
        self.suggest.alpha = 1;
    }
    if(_putSuggest.text.length>10)
    {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示!" message:@"亲!最多只能输入300个字!请您合理安排内容!" preferredStyle:UIAlertControllerStyleAlert];
        
        [self presentViewController:alertController animated:YES completion:nil];
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [self dismissViewControllerAnimated:YES completion:nil];
            

        });
                       }

}



-(void)sender
{
    if(_putSuggest.text.length<5)
    {

        UILabel *notice=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150, 75)];
        notice.layer.cornerRadius=10;
        notice.clipsToBounds=YES;
        notice.text=@"请输入至少5个字";
        notice.textAlignment=NSTextAlignmentCenter;
        notice.center=self.view.center;
        notice.backgroundColor=[UIColor blackColor];
        notice.textColor=[UIColor whiteColor];
        [self.view addSubview:notice];
        [UIView animateWithDuration:2 delay:0 options:nil animations:^{
            notice.alpha=0;
        } completion:^(BOOL finished) {
            
        }];
                         
        
    }
    else
    {

        [UIView animateWithDuration:5 delay:0 options:nil animations:^{
            UILabel *notice=[[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2-75, self.view.frame.size.height/2-37, 150, 75)];
            notice.layer.cornerRadius=10;
            notice.clipsToBounds=YES;
            notice.text=@"正在发送";
            notice.textAlignment=NSTextAlignmentCenter;
//            notice.center=self.view.center;
            notice.backgroundColor=[UIColor blackColor];
            notice.textColor=[UIColor whiteColor];
            [self.view addSubview:notice];
            
           
        } completion:^(BOOL finished) {
            
[self.navigationController popViewControllerAnimated:YES];
            
//            [UIView animateWithDuration:2 delay:5 options:nil animations:^{
//                
//                
//                UILabel *notice2=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 150, 75)];
//                notice2.layer.cornerRadius=10;
//                notice2.clipsToBounds=YES;
//                notice2.text=@"发送成功";
//                notice2.textAlignment=NSTextAlignmentCenter;
//                notice2.center=self.view.center;
//                notice2.backgroundColor=[UIColor blackColor];
//                notice2.textColor=[UIColor whiteColor];
//                [self.view addSubview:notice2];
//            } completion:^(BOOL finished) {

//            }];

        }];
    }
    

}
- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
